package com.example.wechat.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.domain.PageRequest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechat.model.WechatMessage;

@Mapper
public interface WeChatMapper{

	@Select("SELECT * FROM t_wechat_message WHERE from_user = #{from} AND del_flag = false ORDER BY send_time DESC")
	List<WechatMessage> findByFrom(@Param("from") String from);

	@Select("SELECT * FROM t_wechat_message WHERE (from_user = #{from}) AND del_flag = false ORDER BY send_time DESC")
	List<WechatMessage> findByFromOrTo(@Param("from") String from, @Param("to") String to);

	@Select("SELECT * FROM t_wechat_message WHERE from_user = #{from} AND del_flag = false ORDER BY send_time DESC")
	Page<WechatMessage> findPageByFrom(Page<WechatMessage> page, @Param("from") String from);

	@Select("SELECT * FROM t_wechat_message WHERE (from_user = #{from}) AND del_flag = false ORDER BY send_time DESC")
	Page<WechatMessage> findPageByFromOrTo(Page<WechatMessage> page, @Param("from") String from, @Param("to") String to);

	@Select("SELECT * FROM t_wechat_message WHERE content LIKE CONCAT('%', #{content}, '%') AND (from_user = #{user}) AND del_flag = false ORDER BY send_time DESC")
	List<WechatMessage> findByContentContainingAndFromOrTo(@Param("content") String content, @Param("user") String user);

	@Select("SELECT COUNT(*) FROM t_wechat_message WHERE is_read = false AND to_user = #{to} AND del_flag = false")
	int countUnreadByTo(@Param("to") String to);

	@Select("SELECT COUNT(*) FROM t_wechat_message WHERE is_read = false AND to_user = #{user} AND del_flag = false")
	long countUnreadByUser(@Param("user") String user);

	@Select("SELECT COUNT(*) FROM t_wechat_message WHERE to_user = #{user} AND del_flag = false")
	long countByUser(@Param("user") String user);

	@Select("SELECT * FROM t_wechat_message WHERE (from_user = #{from}) AND del_flag = false ORDER BY send_time DESC")
	Page<WechatMessage> findMessagesWithPagination(Page<WechatMessage> page, @Param("from") String from, @Param("to") String to);

	@Select("SELECT * FROM t_wechat_message WHERE (from_user = #{from}) AND send_time BETWEEN #{startTime} AND #{endTime} AND del_flag = false ORDER BY send_time DESC")
	List<WechatMessage> findByFromOrToAndTimestampBetween(@Param("from") String from, @Param("to") String to,
			@Param("startTime") String startTime, @Param("endTime") String endTime);

	@Select("SELECT COUNT(*) FROM t_wechat_message WHERE to_user = #{user} AND from_user = #{from} AND is_read = false AND del_flag = false")
	long countUnreadByFromAndTo(@Param("from") String from, @Param("user") String user);

	@Select("SELECT COUNT(*) FROM t_wechat_message WHERE ((from_user = #{user}) OR (from_user = #{otherUser})) AND del_flag = false")
	long countByFromAndTo(@Param("user") String user, @Param("otherUser") String otherUser);

	@Select("SELECT * FROM t_wechat_message WHERE del_flag = 0 AND id = #{messageId}")
	WechatMessage selectById(@Param("messageId") Long messageId);

	@Insert("INSERT INTO t_wechat_message (from_user, group_id, message_type, content, file_url, file_name, file_size, image_width, image_height, "
			+ "voice_duration, video_duration, is_read, is_recalled, send_time, read_time, recall_time, del_flag, create_time) "
			+ "VALUES (#{fromUser}, #{groupId}, #{messageType}, #{content}, #{fileUrl}, #{fileName}, #{fileSize}, #{imageWidth}, #{imageHeight}, "
			+ "#{voiceDuration}, #{videoDuration}, #{isRead}, #{isRecalled}, #{timestamp}, #{readTime}, #{recallTime}, 0, #{createTime})")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	long insertWechatMessage(WechatMessage message);

	@Update("UPDATE t_wechat_message SET from_user = #{from}, group_id = #{groupId}, message_type = #{messageType}, "
			+ "content = #{content}, file_url = #{fileUrl}, file_name = #{fileName}, file_size = #{fileSize}, image_width = #{imageWidth}, image_height = #{imageHeight}, "
			+ "voice_duration = #{voiceDuration}, video_duration = #{videoDuration}, is_read = #{isRead}, is_recalled = #{isRecalled}, send_time = #{timestamp}, "
			+ "read_time = #{readTime}, recall_time = #{recallTime}, update_time = #{updateTime} WHERE id = #{id}")
	int updateById(WechatMessage message);

}